//
//  JDDGuidanceView.m
//  jingduoduo
//
//  Created by caobo on 15/8/13.
//  Copyright (c) 2015年 totem. All rights reserved.
//

#import "JDDGuidanceView.h"
#import "Live-Header.h"

@interface JDDGuidanceView()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIPageControl *pageControl;

@end

static JDDGuidanceView *shareGuidView = nil;

@implementation JDDGuidanceView{
    NSTimer *timer;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

+(JDDGuidanceView *)shareGuidanceView{
    @synchronized(self){
        if (shareGuidView == nil) {
            shareGuidView = [[self alloc]init];
            [shareGuidView initUI];
        }
    }
    return shareGuidView;
}

-(void)initUI{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTHL, SCREEN_HEIGHTL);

    UIImageView * imv1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTHL, SCREEN_HEIGHTL)];
    UIImage * img1 = [UIImage imageNamed:@"lanch"];
    imv1.image = img1;
    imv1.contentMode = UIViewContentModeScaleAspectFill;
    imv1.userInteractionEnabled = YES;
    UITapGestureRecognizer*tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imv1Actiondo:)];
    [imv1 addGestureRecognizer:tapGesture1];
    imv1.layer.masksToBounds = YES;

    UIImageView * imv2 = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTHL, 0, SCREEN_WIDTHL, SCREEN_HEIGHTL)];
    UIImage * img2 = [UIImage imageNamed:@"lanch_2"];
    imv2.image = img2;
    imv2.contentMode = UIViewContentModeScaleAspectFill;
    imv2.userInteractionEnabled = YES;
    UITapGestureRecognizer*tapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imv2Actiondo:)];
    [imv2 addGestureRecognizer:tapGesture2];
    imv2.layer.masksToBounds = YES;

    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTHL, SCREEN_HEIGHTL);
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTHL*3, SCREEN_HEIGHTL);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled=YES;
    self.scrollView.delegate = self;
    [self.scrollView addSubview:imv1];
    [self.scrollView addSubview:imv2];
    [self addSubview:self.scrollView];
    
    self.pageControl  = [[UIPageControl alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 50, [[UIScreen mainScreen] bounds].size.height - 50, 100, 20)];
    self.pageControl.numberOfPages = 2;
    [self addSubview:self.pageControl];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetWidth = self.scrollView.contentOffset.x;
    int pageNum = offsetWidth / [[UIScreen mainScreen] bounds].size.width;
    self.pageControl.currentPage = pageNum;
}  


-(void)imv1Actiondo:(id)sender{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(timerFired2:) userInfo:nil repeats:NO];

    [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTHL, 0) animated:YES];
    self.scrollView.bouncesZoom = NO;
}

-(void)imv2Actiondo:(id)sender{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    self.hidden = YES;
    self.pageControl = nil;
    [self removeFromSuperview];
}

-(void)timerFired:(id)sender{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerFired2:) userInfo:nil repeats:NO];
    [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTHL, 0) animated:YES];
    self.scrollView.bouncesZoom = NO;
}

-(void)timerFired2:(id)sender{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    self.hidden = YES;
    self.pageControl = nil;
    [self removeFromSuperview];
}

@end
